package jst.householdmanager.database;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import jst.householdmanager.database.table.Items_in_ItemCategories;

public class Items_in_ItemCategoriesDataSource {

	public static final String TABLE_NAME = "Items_in_ItemCategories";
	public static final String COLUMN_ITMCAT_ID = "itInCat_itmCat_ID";
	public static final String COLUMN_ITEM_ID = "itInCat_item_ID";

	public static final String[] ALL_COLUMNS = { COLUMN_ITEM_ID, COLUMN_ITMCAT_ID };

	public static final int[] COLUMN_DATATYPE = { 0, 0 };

	public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " ( " + COLUMN_ITMCAT_ID + " INTEGER, " + COLUMN_ITEM_ID
			+ " INTEGER,  PRIMARY KEY ( " + COLUMN_ITMCAT_ID + " , " + COLUMN_ITEM_ID + ") );";

	public static void save(Items_in_ItemCategories dataRecord) {
		ContentValues values = new ContentValues();
		values.put(COLUMN_ITMCAT_ID, dataRecord.getItmCat_ID());
		values.put(COLUMN_ITEM_ID, dataRecord.getItem_ID());
		SQLiteDataSource.getDatabase().insert(TABLE_NAME, null, values);
	}

	public static void delete(String selection, String[] selectionArgs) {
		SQLiteDataSource.getDatabase().delete(TABLE_NAME, selection, selectionArgs);
	}

	public static Items_in_ItemCategories read(String selection, String[] selectionArgs) {
		Items_in_ItemCategories dataRecord = null;
		Cursor cursor = SQLiteDataSource.getDatabase().query(TABLE_NAME, ALL_COLUMNS, selection, selectionArgs, null, null, null);
		cursor.moveToFirst();
		if (!cursor.isAfterLast()) {
			dataRecord = cursorTo(cursor);
		}
		cursor.close();

		return dataRecord;
	}

	public static List<Items_in_ItemCategories> readAll(String selection, String[] selectionArgs, String groupBy, String having, String orderBy) {
		List<Items_in_ItemCategories> dataRecords = new ArrayList<Items_in_ItemCategories>();
		Cursor cursor = SQLiteDataSource.getDatabase().query(TABLE_NAME, ALL_COLUMNS, selection, selectionArgs, groupBy, having, orderBy);
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			dataRecords.add(cursorTo(cursor));
			cursor.moveToNext();
		}
		cursor.close();

		return dataRecords;
	}

	public static void delete(Items_in_ItemCategories dataRecord) {
		delete(COLUMN_ITMCAT_ID + " = ? and " + COLUMN_ITEM_ID + " = ?",
				new String[] { Integer.toString(dataRecord.getItmCat_ID()), Integer.toString(dataRecord.getItem_ID()) });
	}

	public static void delete(String field, int idValue) {
		delete(field + " = ?", new String[] { Integer.toString(idValue) });
	}

	public static Items_in_ItemCategories read(int itemCatId, int itemId) {
		return read(COLUMN_ITMCAT_ID + " = ? and " + COLUMN_ITEM_ID + " = ?", new String[] { Integer.toString(itemCatId), Integer.toString(itemId) });

	}

	public static List<Items_in_ItemCategories> readAll(String selection, String[] selectionArgs) {
		return readAll(selection, selectionArgs, null, null, null);
	}

	public static List<Items_in_ItemCategories> readAll() {
		return readAll(null, null, null, null, null);
	}

	public static List<Items_in_ItemCategories> readAll(String field, int idValue) {
		return readAll(field + " = ?", new String[] { Integer.toString(idValue) }, null, null, null);
	}

	private static Items_in_ItemCategories cursorTo(Cursor cursor) {
		return new Items_in_ItemCategories(cursor.getInt(0), cursor.getInt(1));
	}

}
